package org.jeecg.modules.dlxj.service.impl;

import org.jeecg.modules.dlxj.entity.DlxjPole;
import org.jeecg.modules.dlxj.mapper.DlxjPoleMapper;
import org.jeecg.modules.dlxj.service.IDlxjPoleService;
import org.jeecg.modules.dlxj.vo.PoleCircuitryVo;
import org.jeecg.modules.dlxj.vo.PoleCodeAndCirCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import java.util.*;

/**
 * @Description: 杆塔表
 * @Author: lzl
 * @Date:   2022-07-20
 * @Version: V1.0
 */
@Service
public class DlxjPoleServiceImpl extends ServiceImpl<DlxjPoleMapper, DlxjPole> implements IDlxjPoleService {

    @Autowired
    public DlxjPoleMapper dlxjPoleMapper;

    /**
     * 通过杆塔id，获得线路名称
     * @param poleIds
     * @return
     */
    @Override
    public Map<String, String> getCirName(List<String> poleIds) {
        List<PoleCircuitryVo> list = this.baseMapper.getCirName(poleIds);

        Map<String, String> res = new HashMap(5);
        list.forEach(item -> {
                    if (res.get(item.getPoleCode()) == null) {
                        res.put(item.getPoleCode(), item.getCircuitryName());
                    } else {
                        res.put(item.getPoleCode(), res.get(item.getPoleCode()) + "," + item.getCircuitryName());
                    }
                }
        );
        return res;    }

    /**
     * 线路禁用
      * @param circuitryCode
     */
    @Override
    public void close(String circuitryCode) {
        dlxjPoleMapper.close(circuitryCode);
    }

    /**
     * 线路启用
     * @param circuitryCode
     */
    @Override
    public void open(String circuitryCode) {
        dlxjPoleMapper.open(circuitryCode);

    }

    @Override
    public void savePoleCodeAndCirCode(DlxjPole dlxjPole) {
        String cirCode = dlxjPoleMapper.getCirCode(dlxjPole.getBelongCirName());
        PoleCodeAndCirCode poleCodeAndCirCode = new PoleCodeAndCirCode();
        poleCodeAndCirCode.setId(dlxjPole.getId());
        poleCodeAndCirCode.setCirCode(cirCode);
        poleCodeAndCirCode.setPoleCode(dlxjPole.getPoleCode());
        poleCodeAndCirCode.setPosition(dlxjPole.getPosition());

        dlxjPoleMapper.savePoleCodeAndCirCode(poleCodeAndCirCode);
    }

    @Override
    public void updatePoleAndCir(DlxjPole dlxjPole) {
        String cirCode = dlxjPoleMapper.getCirCode(dlxjPole.getBelongCirName());
        PoleCodeAndCirCode poleCodeAndCirCode = new PoleCodeAndCirCode();
        poleCodeAndCirCode.setPoleCode(dlxjPole.getPoleCode());
        poleCodeAndCirCode.setCirCode(cirCode);
        poleCodeAndCirCode.setId(dlxjPole.getId());
        poleCodeAndCirCode.setPosition(dlxjPole.getPosition());
        dlxjPoleMapper.updatePoleAndCir(poleCodeAndCirCode);
    }

    @Override
    public void deletePoleAndCir(String id) {
        dlxjPoleMapper.deletePoleAndCir(id);
    }

    @Override
    public void deleteBatchPoleAndCir(String ids) {
        List<String> idList = new ArrayList<>();
        idList = Arrays.asList(ids.split(","));

        dlxjPoleMapper.deleteBatchPoleAndCir(idList);

    }


}
